package kr.hkit.androidcomplete03.ch21;

import kr.hkit.androidcomplete03.MainActivity;
import kr.hkit.androidcomplete03.R;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ActionBarEx extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 액션바를 사용하겠다고 명시 (반드시 setcontentview 하기 전에 쓰기!!)
	//	requestWindowFeature(Window.FEATURE_ACTION_BAR);
		
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.VERTICAL);
		
		LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		
		TextView tv = new TextView(this);
		tv.setText("액션바를 테스트합니다");
		
		layout.addView(tv, params);
		
		final Button btn = new Button(this);
		btn.setText("Hide ActionBar");
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ActionBar ab = getActionBar();
				if(ab.isShowing()){
					ab.hide();
					btn.setText("Show ActionBar");
				} else {
					ab.show();
					btn.setText("Hide ActionBar");
				}
			}
		});
		
		layout.addView(btn, params);
		setContentView(layout);
		
		//
		getActionBar().setHomeButtonEnabled(true);
		getActionBar().setDisplayHomeAsUpEnabled(true);		// 한 단계 위로 올라가는 것.
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.one:
			Toast.makeText(this, "첫 번째 액션 항목 선택", Toast.LENGTH_SHORT).show();
			return true;

		case android.R.id.home:
			
			Intent intent = new Intent(this, Ch21Main.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		MenuInflater inflator = getMenuInflater();
		inflator.inflate(R.menu.actionbarmenu, menu);
		return true;
	}
}
